﻿Imports System.Data.SqlClient
Public Class frmAdeudos
    Dim A As New clsAdeudos
    Dim Usuario As New clsUsuarioPrincipal
    Dim Bitacora As New clsActividad

    Private Sub frmAdeudos_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al Catálogo de Adeudos")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmAdeudos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip2.Cursor = Cursors.Hand
        ToolStrip2.RenderMode = System.Windows.Forms.ToolStripRenderMode.System


        Bitacora.RegistraActividad("Ingresó al Catálogo de Adeudos")
        Usuario.Ubicacion("Catálogo de Adeudos")


        CargaCatalogoConceptos()
        cargaAdeudos()
        txtFolio.Text = (BuscarIntDatoCitra("SELECT MAX(IdAdeudo) as Id from Adeudos")) + 1

    End Sub
    Sub Actualizar()
        Dim intFolio As Integer = 0
        If Me.txtFolio.Text <> "" Then
            intFolio = Me.txtFolio.Text
            A.IDAdeudo = Me.txtFolio.Text
        Else
            intFolio = 0
            A.IDAdeudo = 0
        End If
        A.Buscar()
        Scatter()
        Me.txtFolio.Text = intFolio
        A.IDAdeudo = Me.txtFolio.Text
    End Sub
    Private Sub Scatter()
        Dim Aplicacion As Integer = A.Aplicacion
        Dim Descuento As Integer = A.Descuento

        If Aplicacion = 0 Then
            radFormaEmpleado.Checked = True
        Else
            radFormaTodos.Checked = True
        End If

        If Descuento = 0 Then
            radDescDia.Checked = True
        Else
            radDescTotal.Checked = True
        End If
        txtNombre.Text = A.Nombre
        txtCantidad.Text = A.Cantidad
        lstConcepto.SelectedItem = BuscarStrDatoNominas("SELECT DESCRIPCION FROM NOM10004 WHERE IDCONCEPTO=" & A.IDConcepto)

        If A.Prioridad = 0 Then
            txtPrioridad.Text = (BuscarIntDatoCitra("SELECT MAX(Prioridad) as Id from Adeudos")) + 1
        Else
            txtPrioridad.Text = A.Prioridad
        End If

    End Sub

    Private Sub txtFolio_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtFolio.TextChanged
        Actualizar()
    End Sub

    Private Sub lstListado_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstListado.SelectedIndexChanged
        Dim ID As Integer = BuscarIntDatoCitra("Select IdAdeudo FROM Adeudos WHERE Nombre='" & lstListado.SelectedItem.ToString.Trim & "'")
        txtFolio.Text = ID
    End Sub

    Private Sub radFormaEmpleado_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radFormaEmpleado.CheckedChanged
        If radFormaEmpleado.Checked = True Then
            radDescDia.Enabled = True
            radDescTotal.Enabled = True
            radDescDia.Checked = True
        End If
    End Sub

    Private Sub radFormaTodos_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radFormaTodos.CheckedChanged
        If radFormaTodos.Checked = True Then
            radDescDia.Enabled = True
            radDescTotal.Enabled = False
            radDescDia.Checked = True
        End If
    End Sub

    Private Sub radDescDia_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radDescDia.CheckedChanged
        If radDescDia.Checked = True Then
            txtCantidad.Enabled = True

        End If
    End Sub

    Private Sub radDescTotal_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radDescTotal.CheckedChanged
        If radDescTotal.Checked = True Then
            txtCantidad.Enabled = False
        End If
    End Sub
    Sub cargaAdeudos()
        lstListado.Items.Clear()

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection

        Dim strSql As String = "Select Nombre FROM Adeudos ORDER BY Nombre"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn
        cmdBuscar.CommandText = strSql

        Dim rdBuscar As SqlDataReader
        cnconn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstListado.Items.Add(rdBuscar("Nombre").ToString.Trim)
        Loop
    End Sub
    Sub CargaCatalogoConceptos()
        lstConcepto.Items.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = NominasConnection

        Dim strSql As String = "SELECT Descripcion from NOM10004 Where TipoConcepto<>'P' ORDER BY Descripcion"

        Dim cmdBuscar As New SqlClient.SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql

        Dim rdBuscar As SqlClient.SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstConcepto.Items.Add(rdBuscar("Descripcion").ToString.Trim)
        Loop
    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton5_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click
        txtFolio.Text = 0
        txtFolio.Text = (BuscarIntDatoCitra("SELECT MAX(IdAdeudo) as Id from Adeudos")) + 1
    End Sub

    Private Sub ToolStripButton6_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton6.Click
        If txtNombre.Text = "" Then
            MsgBox("Debes indicar un nombre correcto para el adeudo", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtCantidad.Enabled = True Then
            If txtCantidad.Text = "" Then
                MsgBox("Debes indicar una cantidad valida para el adeudo", MsgBoxStyle.Exclamation, "Aviso")
                Exit Sub
            End If
            If txtCantidad.Text = "0" Then
                MsgBox("Debes indicar una cantidad valida para el adeudo", MsgBoxStyle.Exclamation, "Aviso")
                Exit Sub
            End If
        End If

        If txtPrioridad.Text = 0 Then
            MsgBox("Debes indicar la prioridad del adeudo", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim IDPriori As Integer = BuscarIntDatoCitra("Adeudos", "IdAdeudo", "Prioridad=" & txtPrioridad.Text)
        Dim Adeudo As String = BuscarStrDatoCitra("Adeudos", "Nombre", "IdAdeudo=" & IDPriori)
        Dim ConceptoPrioridad As String

        If IDPriori > 0 Then
            ConceptoPrioridad = BuscarIntDatoCitra("SELECT IdAdeudo FROM Adeudos WHERE Prioridad=" & txtPrioridad.Text & " AND Nombre='" & txtNombre.Text.Trim & "'")
            If ConceptoPrioridad = 0 Then
                MsgBox("La prioridad indicada, ya ha sido asignada al adeudo '" & Adeudo.Trim.Trim & "'", MsgBoxStyle.Exclamation, "Aviso")
                Exit Sub
            End If

        End If

        A.IDAdeudo = txtFolio.Text
        A.Nombre = txtNombre.Text.Trim
        If radFormaEmpleado.Checked = True Then
            A.Aplicacion = 0
        Else
            A.Aplicacion = 1
        End If

        If radDescDia.Checked = True Then
            A.Descuento = 0
        Else
            A.Descuento = 1
        End If

        A.IDConcepto = BuscarIntDatoNominas("SELECT IDCONCEPTO FROM NOM10004 WHERE DESCRIPCION='" & lstConcepto.Text.Trim & "'")
        A.Cantidad = txtCantidad.Text
        A.Prioridad = txtPrioridad.Text

        A.Guardar()
        txtFolio.Text = 0
        txtFolio.Text = (BuscarIntDatoCitra("SELECT MAX(IdAdeudo) as Id from Adeudos")) + 1
        cargaAdeudos()


    End Sub

    Private Sub ToolStripButton2_Click_1(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Close()
    End Sub
End Class